package com.cskaoyan.mall.service;



import com.cskaoyan.mall.bean.*;
import com.cskaoyan.mall.bean.vo.*;
import com.cskaoyan.mall.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zsp
 * 2021/1/9
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    FootprintMapper footprintMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    @Autowired
    FeedbackMapper feedbackMapper;

    @Override
    public Integer getUserTotal() {
        return userMapper.selectAll();
    }

    /**
     *用户管理模块的会员管理
     */
    @Override
    public UserManagerVO<User> queryUserList(Integer page, Integer limit, String username, String mobile, String sort, String order) {

        //分页
        PageHelper.startPage(page, limit);

        //查询
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();

        if (username != null) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (mobile != null) {
            criteria.andMobileLike("%" + mobile + "%");
        }
        userExample.setOrderByClause(sort + " " + order);
        List<User> users = userMapper.selectByExample(userExample);
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        int total = (int) userPageInfo.getTotal();


        return UserManagerVO.data(users,total);
    }

    /**
     *用户管理模块的收货地址
     */
    @Override
    public UserManagerVO<ItemsBean> queryAdressList(Integer page, Integer limit, String name, Integer userId, String sort, String order) {
        //分页
        PageHelper.startPage(page, limit);
        //查询
        List<ItemsBean> addresses = addressMapper.selectAddressByAreaIdCityIdProvinceId(userId,name);
        PageInfo<ItemsBean> addressPageInfo = new PageInfo<>(addresses);
        int total = (int) addressPageInfo.getTotal();

        return UserManagerVO.data(addresses,total);
    }

    /**
     *用户管理模块的会员收藏
     */
    @Override
    public UserManagerVO<Collect> queryCollectList(Integer page, Integer limit, Integer userId, Integer valueId, String sort, String order) {

        //分页
        PageHelper.startPage(page, limit);
        //查询
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        collectExample.setOrderByClause(sort + " " + order);
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
        int total = (int) collectPageInfo.getTotal();

        return UserManagerVO.data(collects,total);
    }

    /**
     *用户管理模块的会员足迹
     */
    @Override
    public UserManagerVO<Footprint> queryFootPrintList(Integer page, Integer limit, Integer userId, Integer goodsId, String sort, String order) {

        //分页
        PageHelper.startPage(page, limit);
        //查询
        FootprintExample footprintExample = new FootprintExample();
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        footprintExample.setOrderByClause(sort + " " + order);
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
        int total = (int) footprintPageInfo.getTotal();
        return UserManagerVO.data(footprints,total);
    }

    /**
     *用户管理模块的搜索历史
     */
    @Override
    public UserManagerVO<SearchHistory> queryHistoryList(Integer page, Integer limit, Integer userId, String keyword, String sort, String order) {

        //分页
        PageHelper.startPage(page, limit);
        //查询
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (keyword != null) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        searchHistoryExample.setOrderByClause(sort + " " + order);
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        PageInfo<SearchHistory> searchHistoryPageInfo = new PageInfo<>(searchHistories);
        int total = (int) searchHistoryPageInfo.getTotal();

        return UserManagerVO.data(searchHistories,total);
    }

    /**
     * 统计报表里的用户统计
     * @return
     */
    @Override
    public UserStatVO userStat() {

        Integer users = userMapper.selectAll();

        ArrayList<UserStatVO.RowsBean> rowsBeans = new ArrayList<>();
        UserStatVO.RowsBean rowsBean = new UserStatVO.RowsBean();
        rowsBean.setUsers(users);
        rowsBeans.add(rowsBean);
        ArrayList<String> strings = new ArrayList<>();
        strings.add("day");
        strings.add("users");
        return UserStatVO.data(strings,rowsBeans);
    }

    /**
     *用户管理模块的意见反馈
     */
    @Override
    public UserManagerVO<Feedback> queryFeedbackList(Integer page, Integer limit, String username, String sort, String order, Integer id) {

        //分页
        PageHelper.startPage(page, limit);
        //查询
        FeedbackExample feedbackExample = new FeedbackExample();
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        if (username != null) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        feedbackExample.setOrderByClause(sort + " " + order);
        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        PageInfo<Feedback> feedbackPageInfo = new PageInfo<>(feedbacks);
        int total = (int) feedbackPageInfo.getTotal();

        return UserManagerVO.data(feedbacks,total);
    }
}
